<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Creating three new variables in a panel dataset in New SAS User</title>
    <link>https://communities.sas.com/t5/New-SAS-User/Creating-three-new-variables-in-a-panel-dataset/m-p/670917#M23341</link>
    <description>&lt;P&gt;Pay attention to details. You will find that&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/462"&gt;@PGStats&lt;/a&gt;' code explicitly removes the PERCENT format, thus displaying the raw value (which is 0.917 for a value displayed as 91.7%).&lt;/P&gt;</description>
    <pubDate>Tue, 21 Jul 2020 11:02:35 GMT</pubDate>
    <dc:creator>Kurt_Bremser</dc:creator>
    <dc:date>2020-07-21T11:02:35Z</dc:date>
    <item>
      <title>Creating three new variables in a panel dataset</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Creating-three-new-variables-in-a-panel-dataset/m-p/670834#M23329</link>
      <description>&lt;P&gt;Hi friends,&lt;/P&gt;
&lt;P&gt;I have a panel dataset which includes weekly data for several brands across four years (250 observations per brand). I appreciate if you help me with doing the following three things (I apologize in advance if I shouldn't ask multiple questions under one topic). Also sorry I cannot share a sample of my data due to NDA.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;1. My dataset includes a column named "brand_name". I want to create a new column ("id") which uniquely identifies each brand. So it should take on the same value for all the 250 rows associated with each brand.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;2. I need a new variable which assigns a number (from 1 to 250) to each observation for each brand. My dataset includes two columns "Year0" and "week". For each brand, my data starts from the 6th week of 2014 up until 52nd week of 2014. Then, from week 0 of 2015 to week 52 of 2015, and so on. In other words, when "Year0" is 2014, "week" starts from 6 all the way to 52. Then for the next row, in which "Year0" is 2015,&amp;nbsp;"week" is 0 (so the variable week starts over with a change in Year0). The new variable should assign 1 whenever Year0 is 2014 and week is 6, and continue this until the last observation, for which it should be 250.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;3. I have several variables which are in percentage (they have been calculated using avg() format=percent7.2 ). I need to convert these to numbers. so for example&amp;nbsp;91.70% should be converted to 0.917.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thanks so much in advance for your help.&lt;/P&gt;</description>
      <pubDate>Mon, 20 Jul 2020 22:56:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Creating-three-new-variables-in-a-panel-dataset/m-p/670834#M23329</guid>
      <dc:creator>AlG</dc:creator>
      <dc:date>2020-07-20T22:56:44Z</dc:date>
    </item>
    <item>
      <title>Re: Creating three new variables in a panel dataset</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Creating-three-new-variables-in-a-panel-dataset/m-p/670860#M23337</link>
      <description>&lt;P&gt;Assuming your data is sorted by brand_name year0 and week :&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
set have;
by brand_name;
if first.brand_name then do;
   id + 1;
   weekId = 0;
   end;
weekId + 1;
format percentA percentB ....; /* Removes the format */
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Removing the format brings the data representation to its original (fractional) form.&lt;/P&gt;</description>
      <pubDate>Tue, 21 Jul 2020 02:17:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Creating-three-new-variables-in-a-panel-dataset/m-p/670860#M23337</guid>
      <dc:creator>PGStats</dc:creator>
      <dc:date>2020-07-21T02:17:32Z</dc:date>
    </item>
    <item>
      <title>Re: Creating three new variables in a panel dataset</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Creating-three-new-variables-in-a-panel-dataset/m-p/670870#M23338</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/462"&gt;@PGStats&lt;/a&gt;&amp;nbsp;Thanks so much.&lt;/P&gt;
&lt;P&gt;The code that you provided elegantly generates the brand_name ID and week ID. However, it does not convert the percentage values to fractional form. For example, I have a variable named "pct_svm_price". Base on your code, I ran the following. However, the format is still percentage. Am I missing something here?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data Final3;
set Final2;
format pct_svm_price; run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;I don't know if this is at all relevant, but as I said before, the percentage variables were originally generated using avg(). For example,&amp;nbsp;pct_svm_price was calculated using the following code:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;avg (case when svm_price= "yes" then 1 else 0 end) format=percent7.2 as pct_svm_price&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 21 Jul 2020 03:55:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Creating-three-new-variables-in-a-panel-dataset/m-p/670870#M23338</guid>
      <dc:creator>AlG</dc:creator>
      <dc:date>2020-07-21T03:55:53Z</dc:date>
    </item>
    <item>
      <title>Re: Creating three new variables in a panel dataset</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Creating-three-new-variables-in-a-panel-dataset/m-p/670917#M23341</link>
      <description>&lt;P&gt;Pay attention to details. You will find that&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/462"&gt;@PGStats&lt;/a&gt;' code explicitly removes the PERCENT format, thus displaying the raw value (which is 0.917 for a value displayed as 91.7%).&lt;/P&gt;</description>
      <pubDate>Tue, 21 Jul 2020 11:02:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Creating-three-new-variables-in-a-panel-dataset/m-p/670917#M23341</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2020-07-21T11:02:35Z</dc:date>
    </item>
    <item>
      <title>Re: Creating three new variables in a panel dataset</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Creating-three-new-variables-in-a-panel-dataset/m-p/670951#M23342</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/11562"&gt;@Kurt_Bremser&lt;/a&gt;&amp;nbsp;I am so sorry, I read your message several times and went back to&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/462"&gt;@PGStats&lt;/a&gt;&amp;nbsp;'s code but couldn't figure out what is going on. BTW, I don't know if this helps or not but the variable&amp;nbsp;&lt;SPAN&gt;pct_svm_price is character variable.&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Tue, 21 Jul 2020 12:41:17 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Creating-three-new-variables-in-a-panel-dataset/m-p/670951#M23342</guid>
      <dc:creator>AlG</dc:creator>
      <dc:date>2020-07-21T12:41:17Z</dc:date>
    </item>
    <item>
      <title>Re: Creating three new variables in a panel dataset</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Creating-three-new-variables-in-a-panel-dataset/m-p/670984#M23345</link>
      <description>&lt;P&gt;Come on, there's even a comment in there that says "Removes the format"!&lt;/P&gt;</description>
      <pubDate>Tue, 21 Jul 2020 13:38:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Creating-three-new-variables-in-a-panel-dataset/m-p/670984#M23345</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2020-07-21T13:38:06Z</dc:date>
    </item>
    <item>
      <title>Re: Creating three new variables in a panel dataset</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Creating-three-new-variables-in-a-panel-dataset/m-p/670987#M23346</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/11562"&gt;@Kurt_Bremser&lt;/a&gt;&amp;nbsp;I see that. That's why I did the following:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data Final3;
set Final2;
format pct_svm_price; run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Am I missing something?&lt;/P&gt;</description>
      <pubDate>Tue, 21 Jul 2020 13:40:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Creating-three-new-variables-in-a-panel-dataset/m-p/670987#M23346</guid>
      <dc:creator>AlG</dc:creator>
      <dc:date>2020-07-21T13:40:15Z</dc:date>
    </item>
    <item>
      <title>Re: Creating three new variables in a panel dataset</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Creating-three-new-variables-in-a-panel-dataset/m-p/670990#M23347</link>
      <description>&lt;P&gt;You want to remove the PERCENT format from the percent.... variables, so you have to name those in the FORMAT statement.&lt;/P&gt;</description>
      <pubDate>Tue, 21 Jul 2020 13:45:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Creating-three-new-variables-in-a-panel-dataset/m-p/670990#M23347</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2020-07-21T13:45:48Z</dc:date>
    </item>
    <item>
      <title>Re: Creating three new variables in a panel dataset</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Creating-three-new-variables-in-a-panel-dataset/m-p/670994#M23348</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/11562"&gt;@Kurt_Bremser&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The variable is&amp;nbsp;pct_svm_price, and I am naming it in the format statement (format pct_svm_price;).&lt;/P&gt;</description>
      <pubDate>Tue, 21 Jul 2020 14:02:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Creating-three-new-variables-in-a-panel-dataset/m-p/670994#M23348</guid>
      <dc:creator>AlG</dc:creator>
      <dc:date>2020-07-21T14:02:03Z</dc:date>
    </item>
    <item>
      <title>Re: Creating three new variables in a panel dataset</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Creating-three-new-variables-in-a-panel-dataset/m-p/671030#M23353</link>
      <description>&lt;P&gt;Then please post the&amp;nbsp;&lt;EM&gt;complete&amp;nbsp;&lt;/EM&gt;log of the step that creates this variable.&lt;/P&gt;
&lt;P&gt;Use the &amp;lt;/&amp;gt; button to post the log.&lt;/P&gt;</description>
      <pubDate>Tue, 21 Jul 2020 15:00:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Creating-three-new-variables-in-a-panel-dataset/m-p/671030#M23353</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2020-07-21T15:00:15Z</dc:date>
    </item>
    <item>
      <title>Re: Creating three new variables in a panel dataset</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Creating-three-new-variables-in-a-panel-dataset/m-p/671077#M23356</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/11562"&gt;@Kurt_Bremser&lt;/a&gt;&amp;nbsp;Below is the log&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;proc sql;
    create table Final as
    select avg (case when svm_price= "yes" then 1 else 0 end) format=percent7.2 as pct_svm_price
from Pre-Final
quit;&lt;/PRE&gt;</description>
      <pubDate>Tue, 21 Jul 2020 16:22:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Creating-three-new-variables-in-a-panel-dataset/m-p/671077#M23356</guid>
      <dc:creator>AlG</dc:creator>
      <dc:date>2020-07-21T16:22:22Z</dc:date>
    </item>
    <item>
      <title>Re: Creating three new variables in a panel dataset</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Creating-three-new-variables-in-a-panel-dataset/m-p/671082#M23357</link>
      <description>&lt;P&gt;To get from a character variable to a numeric variable, use the INPUT function with the PERCENT. informat:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data test;
pct_str = "91.7%";
pct_num = input(pct_str, percent.);
run;

proc print data=test; run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;PRE&gt;Obs. 	pct_str 	pct_num
1 	91.7% 	0.917&lt;/PRE&gt;
&lt;P&gt;Note that you cannot change the type of a variable, you have to create a new one.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you simply want to change the string, convert to a number and back to a string, with a different format:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data test;
pct_str = "91.7%";
pct_str = left(put(input(pct_str, percent.), best.));
run;

proc print data=test; run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;PRE&gt;Obs. 	pct_str
1 	0.917&lt;/PRE&gt;</description>
      <pubDate>Tue, 21 Jul 2020 15:48:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Creating-three-new-variables-in-a-panel-dataset/m-p/671082#M23357</guid>
      <dc:creator>PGStats</dc:creator>
      <dc:date>2020-07-21T15:48:56Z</dc:date>
    </item>
    <item>
      <title>Re: Creating three new variables in a panel dataset</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Creating-three-new-variables-in-a-panel-dataset/m-p/671096#M23359</link>
      <description>Here you create&lt;BR /&gt;pct_svm_brand&lt;BR /&gt;which is nowhere in your previous codes.</description>
      <pubDate>Tue, 21 Jul 2020 16:20:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Creating-three-new-variables-in-a-panel-dataset/m-p/671096#M23359</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2020-07-21T16:20:40Z</dc:date>
    </item>
    <item>
      <title>Re: Creating three new variables in a panel dataset</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Creating-three-new-variables-in-a-panel-dataset/m-p/671099#M23360</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/11562"&gt;@Kurt_Bremser&lt;/a&gt;&amp;nbsp;My bad! That was a typo. I corrected it.&lt;/P&gt;</description>
      <pubDate>Tue, 21 Jul 2020 16:23:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Creating-three-new-variables-in-a-panel-dataset/m-p/671099#M23360</guid>
      <dc:creator>AlG</dc:creator>
      <dc:date>2020-07-21T16:23:13Z</dc:date>
    </item>
    <item>
      <title>Re: Creating three new variables in a panel dataset</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Creating-three-new-variables-in-a-panel-dataset/m-p/671304#M23375</link>
      <description>&lt;P&gt;Then the variable pct_sum_price cannot (I repeat: &lt;STRONG&gt;CANNOT&lt;/STRONG&gt;) be of type character, and you simply need to omit the format= option in the SQL to get the raw value:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data pre_final;
input svm_price $;
datalines;
yes
yes
yes
yes
yes
yes
yes
yes
yes
yes
yes
yes
yes
yes
yes
yes
yes
yes
no
;

proc sql;
create table Final as
  select avg (case when svm_price= "yes" then 1 else 0 end) as pct_svm_price
  from pre_final;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Result in dataset final:&lt;/P&gt;
&lt;PRE&gt;0.9473684211&lt;/PRE&gt;</description>
      <pubDate>Wed, 22 Jul 2020 08:20:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Creating-three-new-variables-in-a-panel-dataset/m-p/671304#M23375</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2020-07-22T08:20:33Z</dc:date>
    </item>
  </channel>
</rss>

